"""
系统状态管理服务
"""
import threading
from config.app_config import SYSTEM_STATUS, SYSTEM_CONFIG

class SystemService:
    """系统状态管理服务类"""
    
    def __init__(self):
        self.status = SYSTEM_STATUS.copy()
        self.config = SYSTEM_CONFIG.copy()
        self.detection_thread = None
    
    def get_status(self):
        """获取系统状态"""
        return self.status.copy()
    
    def get_config(self):
        """获取系统配置"""
        return self.config.copy()
    
    def update_config(self, new_config):
        """更新系统配置"""
        self.config.update(new_config)
        return True
    
    def start_system(self):
        """启动系统"""
        if not self.status['running']:
            self.status['running'] = True
            return True
        return False
    
    def stop_system(self):
        """停止系统"""
        if self.status['running']:
            self.status['running'] = False
            return True
        return False
    
    def is_running(self):
        """检查系统是否运行中"""
        return self.status['running']
    
    def update_detection_stats(self, is_defective=False, defect_type=None):
        """更新检测统计数据"""
        self.status['total_processed'] += 1
        
        if is_defective and defect_type:
            self.status['total_defective'] += 1
            if defect_type in self.status['defect_counts']:
                self.status['defect_counts'][defect_type] += 1
        else:
            self.status['total_normal'] += 1
    
    def reset_stats(self):
        """重置统计数据"""
        self.status.update({
            'total_processed': 0,
            'total_defective': 0,
            'total_normal': 0,
            'defect_counts': {
                'crazing': 0,
                'inclusion': 0,
                'patches': 0,
                'pitted_surface': 0,
                'rolled_in_scale': 0,
                'scratches': 0
            }
        })

# 全局系统服务实例
system_service = SystemService() 